//
//  DWJNetWorkTools.swift
//  Swift网络请求
//
//  Created by 代伟佳 on 16/4/19.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit
import AFNetworking

// 请求方式枚举
enum DWJRequestMethod: Int{
    case GET = 0
    case POST = 1
}

class DWJNetWorkTools: AFHTTPSessionManager {

    
    // 网络请求全局访问点
    static let sharedTools:DWJNetWorkTools = {
        
        let tools = DWJNetWorkTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()

    
    // 网络请求方法
    func request(method:DWJRequestMethod, urlString: String, params:AnyObject?,callback:(response: AnyObject?, error:NSError?)->() ){

        // GET
        if method == .GET{
            self.GET(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                    callback(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    callback(response: nil, error: error)
                    
            })
            
            // POST
        }else{
            self.POST(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                    callback(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    callback(response: nil, error: error)
                    
            })
        }
        
    }
    
}
